package com.xtool.livewallpaper

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.net.Uri
import android.os.IBinder
import androidx.lifecycle.LifecycleObserver
import com.tencent.mmkv.MMKV
import com.xtool.livewallpaper.base.MainApplication
import com.xtool.livewallpaper.base.PrefsHelper

/**
 * @ClassName: WallpaperManager
 * @Description:
 * @Author: Haiyang
 * @Date: 2021/7/27 21:16
 */
class WallpaperNotifyManager {
    companion object {
        val instance by lazy { WallpaperNotifyManager() }
    }

    private var mWallpaperService: IWallPaperInterface? = null
    private val mConnection: ServiceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            mWallpaperService = IWallPaperInterface.Stub.asInterface(service)?.also {
                it.awake()
            }
            this@WallpaperNotifyManager.connectedCallback()
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            mWallpaperService = null
        }
    }

    private var connectedCallback = {}
    fun connectWallpaperProcess(callback: () -> Unit = {}) {
        try {
            if (mWallpaperService == null) {
                this.connectedCallback = callback
                val intent = Intent(MainApplication.gContext, WallPaperAliveService::class.java)
                MainApplication.gContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE)
            } else {
                callback()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun onDestroy() {
        try {
            MainApplication.gContext.unbindService(mConnection)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun setVideoWallpaper(src: Uri, isMute: Boolean = true): Boolean {
        return try {
            PrefsHelper.isMute = isMute
            mWallpaperService?.setVideoWallpaper(src, isMute)
            true
        } catch (e: Exception) {
            false
        }
    }

    fun setCameraWallpaper(isFront: Boolean): Boolean {
        return try {
            mWallpaperService?.setCameraWallpaper(isFront)
            true
        } catch (e: Exception) {
            false
        }
    }

    fun setGifWallpaper(src: String): Boolean {
        return try {
            mWallpaperService?.setGifWallpaper(src)
            true
        } catch (e: Exception) {
            false
        }
    }

    fun setMute(isMute: Boolean): Boolean {
        return try {
            mWallpaperService?.setMute(isMute)
            true
        } catch (e: Exception) {
            false
        }
    }

    fun pause() {
        mWallpaperService?.pause()
    }

    fun resume() {
        mWallpaperService?.play()
    }

    fun isVideoAlive(): Boolean {
        return mWallpaperService?.isVideoAlive ?: false
    }

    fun isCameraAlive(): Boolean {
        return mWallpaperService?.isCameraAlive ?: false
    }

    fun isGifAlive(): Boolean {
        return mWallpaperService?.isGifAlive ?: false
    }

}